package com.yhn.init.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhn.init.common.DeleteRequest;
import com.yhn.init.common.ErrorCode;
import com.yhn.init.exception.BusinessException;
import com.yhn.init.exception.ThrowUtils;
import com.yhn.init.mapper.DetailsMapper;
import com.yhn.init.model.dto.details.DetailsAddRequest;
import com.yhn.init.model.dto.details.DetailsQueryRequest;
import com.yhn.init.model.dto.details.DetailsUpdateRequest;
import com.yhn.init.model.entity.Details;
import com.yhn.init.model.vo.DetailsVO;
import com.yhn.init.service.DetailsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service("detailsService")
public class DetailsServiceImpl extends ServiceImpl<DetailsMapper, Details> implements DetailsService {

    @Override
    public Page<DetailsVO> queryPage(DetailsQueryRequest detailsQueryRequest) {
        if (detailsQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = detailsQueryRequest.getCurrent();
        long size = detailsQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Details> detailsPage = this.page(new Page<>(current, size),
                this.getQueryWrapper(detailsQueryRequest));
        Page<DetailsVO> detailsVOPage = new Page<>(current, size, detailsPage.getTotal());
        List<DetailsVO> detailsVO = this.getDetailsVO(detailsPage.getRecords());
        detailsVOPage.setRecords(detailsVO);
        return detailsVOPage;
    }


    @Override
    public QueryWrapper<Details> getQueryWrapper(DetailsQueryRequest detailsQueryRequest) {
        QueryWrapper<Details> queryWrapper = new QueryWrapper<>();
        // 如果请求对象为空，直接返回一个空的查询包装器
        if (detailsQueryRequest == null) {
            return queryWrapper;
        }
        // 从请求对象中获取各项查询条件
        Long id = detailsQueryRequest.getId();
        queryWrapper.eq(id != null && id > 0, "id", id);
        return queryWrapper;
    }

    /**
     * 获取脱敏对象集合
     *
     * @param detailsList 查询请求
     * @return 脱敏对象集合
     */
    @Override
    public List<DetailsVO> getDetailsVO(List<Details> detailsList) {
        if (CollUtil.isEmpty(detailsList)) {
            return new ArrayList<>();
        }
        return detailsList.stream().map(this::getDetailsVO).collect(Collectors.toList());
    }


    /**
     * 将对象转化为脱敏对象
     *
     * @param details 原对象
     * @return 脱敏对象
     */
    @Override
    public DetailsVO getDetailsVO(Details details) {
        if (details == null) {
            return null;
        }
        DetailsVO detailsVO = new DetailsVO();
        BeanUtils.copyProperties(details, detailsVO);
        return detailsVO;
    }

    /**
     * 根据ID获取信息
     *
     * @param id id
     * @return 实体信息
     */
    @Override
    public DetailsVO getInfoById(Long id) {
        Details details = this.getById(id);
        if (details == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "没有对应的信息");
        }
        return getDetailsVO(details);
    }

    /**
     * 保存信息
     *
     * @param detailsAddRequest 添加请求
     * @return 新生成的ID
     */
    @Override
    @Transactional
    public Long saveDetails(DetailsAddRequest detailsAddRequest) {
        if (detailsAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Details details = new Details();
        BeanUtils.copyProperties(detailsAddRequest, details);
        boolean result = this.save(details);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return details.getId();
    }


    /**
     * 修改信息
     *
     * @param detailsUpdateRequest 修改请求
     * @return 是否修改成功
     */
    @Override
    @Transactional
    public Boolean updateDetails(DetailsUpdateRequest detailsUpdateRequest) {
        if (detailsUpdateRequest == null || detailsUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Details details = new Details();
        BeanUtils.copyProperties(detailsUpdateRequest, details);
        long id = detailsUpdateRequest.getId();
        // 判断是否存在
        Details oldDetails = this.getById(id);
        ThrowUtils.throwIf(oldDetails == null, ErrorCode.NOT_FOUND_ERROR);
        boolean result = this.updateById(details);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return result;
    }

    /**
     * 删除信息
     *
     * @param deleteRequest 删除请求
     * @return 是否修改成功
     */
    @Override
    public Boolean deleteById(DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long id = deleteRequest.getId();
        // 判断是否存在
        boolean b = this.removeById(id);
        ThrowUtils.throwIf(!b, ErrorCode.NOT_FOUND_ERROR);
        return b;
    }


    /**
     * 批量删除
     *
     * @param ids 批量删除的IDS
     * @return 是否删除成功
     */
    @Override
    @Transactional
    public Boolean deleteBatchByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = this.removeByIds(Arrays.asList(ids));
        ThrowUtils.throwIf(!b, ErrorCode.NOT_FOUND_ERROR);
        return b;
    }
}
